
jQuery(function(){
	initRemoteEvents();
	initDelete();
});

function initRemoteEvents()
{
	jQuery('a.remote').unbind('click');
	jQuery('a.remote').click(function(ev){
		
		ev.preventDefault();
		modalMsg.show('Loading ...');
		
		//remove tinyMCE instance if we got one
		if (jQuery('#editor textarea#body').get(0))
				tinyMCE.execCommand('mceRemoveControl', false, 'body');
		
		
		
		var url = jQuery(this).attr('href');
		var reg = /;/;
		var delim = (reg.test(url)) ? '&' : ';';
		jQuery('#editor').load(url + delim + 'inline', function(){
			modalMsg.hide();
			if (jQuery('#editor textarea#body').get(0))
				tinyMCE.execCommand('mceAddControl', false, 'body');


			
			if (jQuery('#editor form').get(0))		//add submit action to form
			{
				jQuery('#editor form.classic').submit(function(ev){
					//ev.preventDefault();
					tinyMCE.triggerSave();
					var error = 0;
					
					jQuery('#editor form.classic .required').each(function(i){
						if (jQuery.trim(jQuery(this).val()) == '' && jQuery.trim(jQuery(this).html()) == '')
							error = 1;
					});
					if (error)
					{
						humanMsg.displayMsg('<strong>Required fields left empty !!!</strong>');
						return false;
					}
				});
				
				
				jQuery('#editor form.remote').submit(function(ev){
					ev.preventDefault();
					tinyMCE.triggerSave();
					var error = 0;
					jQuery('#editor form.remote .required').each(function(i){
						if (jQuery.trim(jQuery(this).val()) == '' && jQuery.trim(jQuery(this).html()) == '')
							error = 1;
					});
					if (error)
						humanMsg.displayMsg('<strong>Required fields left empty !!!</strong>');
					else
					{
						var url = jQuery('#editor form.remote').attr('action');
						var reg = /;/;
						var delim = (reg.test(url)) ? '&' : ';';
						
						
						var data = jQuery('#editor form.remote .input').serialize();
						modalMsg.show('Loading ...');
						jQuery.post(url + delim + 'inline', {'submit': 1, 'data': data}, function(resp){
							var json = eval('(' + resp + ')');
							for (i in json.callback)
								eval(json.callback[i]);

							modalMsg.hide();
							
						});
					}
				})
			}
		});
	});
}

function initDelete()
{
	jQuery('#delete-form').submit(function(ev){
		ev.preventDefault();
		if (confirm('Delete selected blog items?'))
		{
			modalMsg.show('Loading ...');
			
			
			var url = jQuery(this).attr('action');
			var reg = /;/;
			var delim = (reg.test(url)) ? '&' : ';';
			url = url + delim + 'inline';
			
			var ids = jQuery(this).find('input:checked').serialize();
			
			jQuery.post(url, {'submit': 1, 'data':ids}, function(data){
				var json = eval('(' + data + ')');
				for (i in json.callback)
					eval(json.callback[i]);
				
				modalMsg.hide();
			})
		}
	})	
}


function updateBlogItems()
{
	jQuery('#items-list').load('/admin/blog/getitems.html;inline', null, function(){
		initRemoteEvents();
		initDelete();
	});
	
}


function addBlogItemList(id)
{
	jQuery('#items-list #item-'+id+' a').click();
}

function updatePortfolioItems()
{
	jQuery('#items-list').load('/admin/portfolio/getitems.html;inline', null, function(){
		initRemoteEvents();
		initDelete();
	});
}

